package com.voxel.sense.infomsg.module.map.monitor.traceRecord;

import com.example.com.common.factory.data.DataSource;
import com.voxel.sense.infomsg.base.RecyclerViewPresenter;
import com.voxel.sense.infomsg.net.helper.TaskManagerNetHelper;

import java.util.ArrayList;
import java.util.List;

import cyy.example.com.facotry.constants.Constants;
import cyy.example.com.facotry.model.api.response.MyTaskListInfoEntity;
import cyy.example.com.facotry.net.RequestIdFactory;
import cyy.example.com.facotry.share.SPEngine;

/**
 * Created by ChenYiYao on 2018/5/14.
 */

public class TaskTracePresenter extends RecyclerViewPresenter<TaskTraceContract.View> implements TaskTraceContract.Presenter, DataSource.Callback<MyTaskListInfoEntity> {
    protected int CURRENT_PAGE_SIZE = 1;//加载数据的页数

    public TaskTracePresenter(TaskTraceContract.View view) {
        super(view);
    }

    @Override
    public void doLoadData() {
       startRefreshData();
    }

    @Override
    public void doLoadMoreData() {
        startLoadMoreData();
    }

    @Override
    protected void loadData() {
        netLoadTask(1);
    }

    @Override
    protected void loadMoreData() {
        netLoadTask(CURRENT_PAGE_SIZE + 1);
    }


    private void netLoadTask(int numb) {
        //获取已接收的任务
        String area = SPEngine.getSPEngine().getUserInfo().getArea();
        List<String> areas = new ArrayList<>();
        areas.add(area);
        TaskManagerNetHelper.queryMyTaskInfoList(numb, Constants.TASK_ACTION_TYPE, Constants.DEFUALT_SORT_TYPE,
                areas, null, this);
    }


    @Override
    public void onDataLoaded(MyTaskListInfoEntity data, int reqId) {
        if (mView == null)
            return;
        if (reqId == RequestIdFactory.GET_TASK_INFO_LIST) {
            //获取任务列表
            loadTaskListFinish(data);
        }
    }

    private void loadTaskListFinish(MyTaskListInfoEntity entity) {
        MyTaskListInfoEntity data = entity;
        if (data != null) {
            CURRENT_PAGE_SIZE = data.getBody().getPageNum();
            loadDataComplete(data.getBody().getTaskInfoList());
        } else {
            LoadDataFailure();
        }
    }

    @Override
    public void onDataNotAvailable(String strRes, int reqId) {
        if (mView == null)
            return;
    }

}
